/*qVKR0emRQh6rPu+hvsbXDk+VfJzZGWH5Wd7LZ17sh2vBtDzaP/XlVZPfhWmGq4fX*/
import React, { Component } from 'react';
import {base,toast,ajax} from 'nc-lightapp-front';
let {NCTabs,NCButton } = base;
let NCTabPane = NCTabs.NCTabPane;
/**
 * liupzhc 联系人 卡片界面
 *
 *  props:{
 *      form:this.props.form,
 *      formId:your linkman form id,
 *  }
 */
export default class Linkman extends Component{
    constructor(props){
        super(props);
        this.state = {
            ...this.props
        }
    }

    /**
     * 联系人 弹出来就默认可以编辑
     */
    componentDidMount(){
        let status = this.props.formStatus ? this.props.formStatus : 'edit'
        this.props.form.setFormStatus(this.state.formId, status);
    }
    componentWillReceiveProps(newProps){
        this.state = {
            ...newProps
        }
    }
    /**
     * 弹出框表单编辑后事件
     */
    onAfterFormEvent(){

    }

    assignOrg =()=>{
        if(this.state.form.getFormItemsValue("linkman","cell").value == undefined){
            toast({title:this.props.Lang["10140SUG-000245"],color:'danger'})/* 国际化处理： 未填写手机号无法分配组织权限！*/
            return;
        }

        this.state.assignOrg(this.state.form.getFormItemsValue("linkman","cell").value);
        this.state.refreshOrg(this.state.form.getFormItemsValue("linkman","cell").value);
    }
    refreshOrg =()=>{
        this.state.refreshOrg(this.state.form.getFormItemsValue("linkman","cell").value);
    }

    deleteOrgs = () =>{
        let val = this.props.table.getCheckedRows("SubjectOrg");
        if(val.length<1){
            toast({content: this.props.Lang["10140SUG-000246"],color:'danger'});//请选择数据
        }else{
            let orgs = "";
            for(let i = 0;i<val.length;i++){
                orgs = orgs + (orgs==""?"":",")+ val[i].data.values.pk_org.value;
            }
            this.deleteOrg(orgs);
        }

    }

     /**
     * 删除移动用户的组织权限
     */
    deleteOrg = (org)=>{
        let phone = this.props.form.getFormItemsValue(this.state.formId,'cell').value;
        console.log('phone====',phone);
        let _this = this;
        ajax({
            url:'/nccloud/baseapp/mobile/orgdelete.do',
            data:{
              phone:phone,
              org:org
            },
            async: false,
            success : (res) => {
                toast({content:this.props.Lang["10140SUG-000244"],color:'success'});//删除成功
                this.state.refreshOrg(this.state.form.getFormItemsValue("linkman","cell").value);
            },
            error: (msg) => {
                toast({content:msg.message,color:'danger'});
            }
        })
    }

    render(){
        let { form ,formId} = this.state;
        let {createForm} = form;
        const {table} = this.props;
        let {createSimpleTable } = table;
        //000243-已关联的组织 000242关联组织 000225-删除
        return(<div id='portalContainter09'>
            {createForm(formId, {
                onAfterEvent: this.onAfterFormEvent.bind(this)
            })}
              <NCTabs navtype="turn" contenttype="moveleft" defaultActiveKey="1"
							id='NCTabs_role' >
                    <NCTabPane tab={this.props.Lang["10140SUG-000243"]} key="1">
                        <div className="uapbd-shoulder-button-area button-app-wrapper">
                            {!this.props.disAssign && <NCButton type={'primary'} style={{margin: '5px'}} onClick = {()=>this.assignOrg()} >{this.props.Lang["10140SUG-000242"]}</NCButton>}
                            {!this.props.disAssign && <NCButton type={'primary'} style={{margin: '5px'}} onClick = {()=>this.deleteOrgs()} >{this.props.Lang["10140SUG-000225"]}</NCButton>}
                            <NCButton bordered className="refresh-component" style={{margin: '5px'}} onClick = {()=>this.refreshOrg()} ><i className="iconfont icon-shuaxin1"/></NCButton>
                        </div>
                    {createSimpleTable('SubjectOrg', {
                        showIndex: true,
                        showCheck: true,
                    })}
                    </NCTabPane>
                </NCTabs>
            </div>)
    }
}




/*qVKR0emRQh6rPu+hvsbXDk+VfJzZGWH5Wd7LZ17sh2vBtDzaP/XlVZPfhWmGq4fX*/
